package com.xuecheng.manage_media_process.mq;

import com.alibaba.fastjson.JSON;
import com.xuecheng.framework.domain.media.MediaFile;
import com.xuecheng.framework.domain.media.MediaFileProcess_m3u8;
import com.xuecheng.framework.utils.HlsVideoUtil;
import com.xuecheng.framework.utils.Mp4VideoUtil;
import com.xuecheng.manage_media_process.dao.MediaFileRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
 * @author ShengJinFeng
 * @version V1.0
 * @ClassName MediaProcessTask
 * @Description (视频处理类，将avi转map4，再将m'p)
 * @date 2020/3/2 10:59
 * @since V1.0
 */
@Component
public class MediaProcessTask {

    private static final Logger LOGGER = LoggerFactory.getLogger(MediaProcessTask.class);

    @Value("${xc-service-manage-media.ffmpeg-path}")
    private String ffmpeg_path;

    @Value("${xc-service-manage-media.server-path}")
    private String server_path;//上传视频存放得根目录

    @Autowired
    private MediaFileRepository mediaFileRepository;

    /**
     * @Description 监听消息队列中的消息，对视频进行处理
     * @author ShengJinFeng
     * @date 2020/3/2
     */
    @RabbitListener(queues = "${xc-service-manage-media.mq.queue-media-video-processor}",containerFactory = "customContainerFactory")
    public void receiveMediaProcessTask(String msg){
        Map msgMap = JSON.parseObject(msg, Map.class);
        LOGGER.info("receive media process task msg:{}",msgMap);

        String mediaId = (String) msgMap.get("mediaId");
        Optional<MediaFile> optional = mediaFileRepository.findById(mediaId);
        if (!optional.isPresent()){
            return;
        }
        MediaFile mediaFile = optional.get();
        //校验视频类型，如果不是avi则不处理，如果是avi才处理
        String fileType = mediaFile.getFileType();
        if (fileType == null || !fileType.equals("avi")){
            mediaFile.setProcessStatus("303004");//处理状态为无需处理
            mediaFileRepository.save(mediaFile);
            return;
        }else{
            mediaFile.setProcessStatus("303001");//处理状态为未处理
            mediaFileRepository.save(mediaFile);
        }
        //将avi视频文件专为mp4
        String videoPath = server_path+mediaFile.getFilePath()+mediaFile.getFileName();//需要转化的视频路径
        String mp4Name = mediaFile.getFileId()+".mp4";//转化后mp4的名称
        String mp4FolderPath = server_path+mediaFile.getFilePath();
        Mp4VideoUtil videoUtil = new Mp4VideoUtil(ffmpeg_path,videoPath,mp4Name,mp4FolderPath);
        String result = videoUtil.generateMp4();
        if (result == null || !result.equals("success")){
            //操作失败写入处理日志
            mediaFile.setProcessStatus("303003");//处理状态为处理失败
            MediaFileProcess_m3u8 process_m3u8 = new MediaFileProcess_m3u8();
            process_m3u8.setErrormsg(result);
            mediaFile.setMediaFileProcess_m3u8(process_m3u8);
            mediaFileRepository.save(mediaFile);
            return;
        }
        //生成m3u8...
        videoPath = server_path+mediaFile.getFilePath()+mp4Name;//现在需要转换的是mp4,所以是mp4的地址
        String m3u8Name = mediaFile.getFileId()+".m3u8";//转化后m3u8的文件名
        String m3u8FolderPath = server_path+mediaFile.getFilePath()+"hls/";//m3u8文件存放的文件夹路径
        HlsVideoUtil hlsVideoUtil = new HlsVideoUtil(ffmpeg_path,videoPath,m3u8Name,m3u8FolderPath);
        result = hlsVideoUtil.generateM3u8();
        if (result == null || !result.equals("success")){
            //操作失败写入处理日志
            mediaFile.setProcessStatus("303003");//处理状态为处理失败
            MediaFileProcess_m3u8 process_m3u8 = new MediaFileProcess_m3u8();
            process_m3u8.setErrormsg(result);
            mediaFile.setMediaFileProcess_m3u8(process_m3u8);
            mediaFileRepository.save(mediaFile);
            return;
        }
        //操作成功，更新状态为处理成功
        mediaFile.setProcessStatus("303002");
        List<String> ts_list = hlsVideoUtil.get_ts_list();
        MediaFileProcess_m3u8 process_m3u8 = new MediaFileProcess_m3u8();
        process_m3u8.setTslist(ts_list);
        mediaFile.setMediaFileProcess_m3u8(process_m3u8);
        mediaFile.setFileUrl(mediaFile.getFilePath()+"hls/"+m3u8Name);
        mediaFileRepository.save(mediaFile);
    }

}
